import React, { PureComponent } from "react"
import { isNull } from "../../utils/helper"
import { connect } from "dva";
@connect(({ form }, { formDataKey }) => {
  return ({
    ...form[formDataKey]
  })
})
export default class MyApis extends PureComponent {
  constructor(props) {
    super(props);
    this.timer = null;
    let { isSearch } = props;
    this.state = {
      show: isSearch
    }
  }

  static propTypes = {
  }

  watchStr(arr, oldFD = {}, newFD = {}) {
    let oldStr = "", newStr = "";
    if (!arr || arr.length == 0) {
      return false
    }
    else {
      arr.map(item => {
        let key = item.store_field || item.key;
        oldStr += "," + oldFD[key];
        newStr += "," + newFD[key];
      });
    }
    return oldStr !== newStr
  }
  componentWillReceiveProps(next) {
    const { id, api, source_type, dispatch, formDataKey, options } = next;
    let oldFD = this.props.formData || {};
    let newFD = next.formData || {};
    //如果source_type==="option" 就不监听formData
    // refreshing 变化的时候也会触发刷新 
    if (this.watchStr(api.params, oldFD, newFD)) {
      dispatch({
        type: "form/query",
        payload: {
          apiInfo: api,
          data: newFD
        }
      })
    }
  }

  componentDidMount() {

  }
  render() {
    return null;
  }
  componentWillUnmount() {
    clearInterval(this.timer)
  }
}